package com.itheima.demo01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class ReceiveMassageDemo {
    public static void main(String[] args) throws IOException {
        //利用UPD 协议接收数据

        //1.创建 DatagramSocket 对象
        DatagramSocket ds = new DatagramSocket(10086);

        //2.打包
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);

        ds.receive(dp);

        InetAddress address = dp.getAddress();
        System.out.println(address);

        byte[] data = dp.getData();
        int len = dp.getLength();
        System.out.println(new String(data,0,len));

        int port = dp.getPort();
        System.out.println(port);

        ds.close();
    }
}
